//
//  UUSupplierManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUSupplierManager.h"
#import "UULoginManager.h"
#import <UUFoundation/DKDataManager.h>
#import "UUSupplierUtility.h"
#import <UUFoundation/UUIncrementDataManager.h>
#import "UUSupplierRegisterRequestModel.h"
#import "UUSupplierTotalIncomeRequestModel.h"
#import "UUSupplierRegisterInfoRequestModel.h"
#import "UUSupplierUpdateRegisterInfoRequestModel.h"
#import "UUSupplierPeakSeasonRequestModel.h"
#import "UUSupplierPeakSeasonDeleteRequestModel.h"
#import "UUSupplierPeakSeasonAddRequestModel.h"

@interface UUSupplierManager ()

@property (nonatomic, strong) UUIncrementDataManager *peakSeasonDataManager;

@end

@implementation UUSupplierManager

- (void)registerSupplierWithMobile:(NSString *)mobile
                            cityId:(NSString *)cityId
                           company:(NSString *)company
                           address:(NSString *)address
                           contact:(NSString *)contact
                     contactMobile:(NSString *)contactMobile
             transportationLicense:(NSString *)transportationLicense
                   businessLicense:(NSString *)businessLicense
                       idCardFront:(NSString *)idCardFront
                        idCardBack:(NSString *)idCardBack
                     completeBlock:(void (^)(NSError *))completeBlock {
    UUSupplierRegisterRequestModel *requestModel = [UUSupplierRegisterRequestModel new];
    requestModel.mobile = mobile;
    requestModel.cityId = cityId;
    requestModel.company = company;
    requestModel.address = address;
    requestModel.contact = contact;
    requestModel.contactMobile = contactMobile;
    requestModel.transportationLicense = transportationLicense;
    requestModel.businessLicense = businessLicense;
    requestModel.idCardFront = idCardFront;
    requestModel.idCardBack = idCardBack;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            UUSupplierRegisterResponseModel *registerResponseModel = (UUSupplierRegisterResponseModel *)responseModel;
            [GetLoginManager() loginWithUserId:registerResponseModel.userId token:registerResponseModel.token];
            completeBlock(nil);
        } else {
            completeBlock(error);
        }
    }];
}

- (void)asyncFetchTotalIncomeWithCompleteBlock:(void (^)(NSNumber *, NSError *))completeBlock {
    UUSupplierTotalIncomeRequestModel *requestModel = [UUSupplierTotalIncomeRequestModel new];
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            UUSupplierTotalIncomeResponseModel *totalIncomeResponseModel = (UUSupplierTotalIncomeResponseModel *)responseModel;
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, totalIncomeResponseModel.totalIncome, nil);
        } else {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, error);
        }
    }];
}

- (void)asyncFetchSupplierRegisterInfoWithSupplierId:(id)supplierId
                                       completeBlock:(void (^)(UUSupplierRegisterInfo *, NSError *))completeBlock {
    UUSupplierRegisterInfoRequestModel *requestModel = [UUSupplierRegisterInfoRequestModel new];
    requestModel.id = supplierId;
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            UUSupplierRegisterInfo *registerInfo = (UUSupplierRegisterInfo *)responseModel;
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, registerInfo, nil);
        } else {
            BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, nil, error);
        }
    }];
}

- (void)asyncUpdateSupplierRegisterInfoWithSupplierId:(id)supplierId
                                               cityId:(NSString *)cityId
                                              company:(NSString *)company
                                              address:(NSString *)address
                                              contact:(NSString *)contact
                                        contactMobile:(NSString *)contactMobile
                                transportationLicense:(NSString *)transportationLicense
                                      businessLicense:(NSString *)businessLicense
                                          idCardFront:(NSString *)idCardFront
                                           idCardBack:(NSString *)idCardBack
                                    completeBlock:(void (^)(NSError *))completeBlock {
    UUSupplierUpdateRegisterInfoRequestModel *requestModel = [UUSupplierUpdateRegisterInfoRequestModel new];
    requestModel.supplierId = supplierId;
    requestModel.cityId = cityId;
    requestModel.company = company;
    requestModel.address = address;
    requestModel.contact = contact;
    requestModel.contactMobile = contactMobile;
    requestModel.transportationLicense = transportationLicense;
    requestModel.businessLicense = businessLicense;
    requestModel.idCardFront = idCardFront;
    requestModel.idCardBack = idCardBack;
    
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)asyncUpdatePeakSeasons {
    WEAK_SELF();
    [self.peakSeasonDataManager asyncIncrementUpdateWithCompleteBlock:^(BOOL hadUpdate, NSArray<id> *insert, NSArray<id> *delete, NSError *error) {
        if (hadUpdate) {
            [weakSelf notifyObserversWithSelector:@selector(supplierManagerDidUpdatePeakSeasons) withObject:nil];
        }
    }];
}

- (NSArray<id> *)fetchPeakSeasons {
    NSArray<NSNumber *> *peakSeasons = [UUSupplierPeakSeasonModel searchColumn:@"id" where:nil orderBy:@"updateTime desc" offset:0 count:0];
    return peakSeasons;
}

- (UUSupplierPeakSeasonModel *)fetchPeakSeasonModelWithId:(NSNumber *)id {
    return (UUSupplierPeakSeasonModel *)[self.peakSeasonDataManager fetchModelWithPrimaryKeyValue:id];
}

- (void)asyncUpdatePeakSeasonWithPeakSeasonId:(NSNumber *)peakSeasonId
                                         name:(NSString *)name
                                        dates:(NSArray<NSString *> *)dates
                                completeBlock:(void (^)(NSError *error))completeBlock {
    UUSupplierPeakSeasonAddRequestModel *requestModel;
    if (peakSeasonId == nil) {
        requestModel = [UUSupplierPeakSeasonAddRequestModel new];
    } else {
        requestModel = [UUSupplierPeakSeasonUpdateRequestModel new];
        ((UUSupplierPeakSeasonUpdateRequestModel *)requestModel).peakSeasonId = peakSeasonId;
    }
    
    requestModel.name = name;
    requestModel.dates = dates;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdatePeakSeasons];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)asyncDeletePeakSeasonWithId:(NSNumber *)id completeBlock:(void (^)(NSError *error))completeBlock {
    UUSupplierPeakSeasonDeleteRequestModel *requestModel = [UUSupplierPeakSeasonDeleteRequestModel new];
    requestModel.peakSeasonId = id;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdatePeakSeasons];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

#pragma mark - Private methods

- (UUIncrementDataManager *)peakSeasonDataManager {
    if (_peakSeasonDataManager == nil) {
        _peakSeasonDataManager = [UUIncrementDataManager managerWithConfiguration:
                                  [UUIncrementDataManagerConfiguration configurationWithRequestModelClass:[UUSupplierPeakSeasonRequestModel class]
                                                                                           storeContainer:[UUSupplierUtility supplierDefaults]]];
    }
    
    return _peakSeasonDataManager;
}

@end
